import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  const hasToken = getToken()
  console.log('hasToken:'+hasToken,1231313)
  if (hasToken) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next()
      // next({ path: '/' })
      NProgress.done()
    } else {

      const routes = store.getters.permission_routes && store.getters.permission_routes.length > 0

      console.log(store.getters.permission_routes,1213);
      if (routes) {
        // store.dispatch('user/getCount')
        if (to.path === '/') {
          next()
          // next({name: store.getters.console_index_name})
        } else {
          next()
        }
      } else {
        try {
            const menu=[
              {
                "level": 1,
                "caption": "公司信息",
                "summary": "根菜单",
                "name": "Company",
                "sub": [
                  {
                    "level": 2,
                    "caption": "门店列表",
                    "summary": "门店列表",
                    "name": "CompanyList",
                    "url": ""
                  },
                  {
                    "level": 2,
                    "caption": "公司车辆",
                    "summary": "公司车辆",
                    "name": "CompanyCar",
                    "url": ""
                  }
                ],
              },{
                "level": 1,
                "sub": [
                  {
                    "level": 2,
                    "caption": "派派预约",
                    "summary": "派派预约",
                    "name": "DispatchingList",
                    "url": ""
                  },
                  {
                    "level": 2,
                    "caption": "服务工单",
                    "summary": "服务工单",
                    "name": "DispatchingServiceSheet",
                    "url": ""
                  }
              ],
              "caption": "派工管理",
              "summary": "根菜单",
              "name": "Dispatching",
            },]
          // const menu = await store.dispatch('user/getMenu')
          const accessRoutes = await store.dispatch('permission/generateRoutes', menu)
          router.addRoutes(accessRoutes)
          // next();
          next({ ...to, replace: true })
        } catch (error) {
          // remove token and go to login page to re-login
          await store.dispatch('user/resetToken')
          Message.error(error || 'Has Error')
          next(`/login?redirect=${to.path}`)
          NProgress.done()
        }
      }
    }
      // const hasGetUserInfo = store.getters.name
      // if (hasGetUserInfo) {
      //
      //   const menu=[{
      //     "id": 27,
      //     "createDate": "2019-08-19 16:22:47",
      //     "level": 1,
      //     "childCount": 3,
      //     "sub": [{
      //       "id": 264,
      //       "createDate": "2019-12-16 10:04:33",
      //       "parentID": 27,
      //       "level": 2,
      //       "childCount": 0,
      //       "appID": 1,
      //       "caption": "商户档案",
      //       "summary": "商户档案",
      //       "name": "OrganizationArchive",
      //       "url": ""
      //     },],
      //     "appID": 1,
      //     "caption": "商户管理",
      //     "summary": "根菜单",
      //     "name": "Organization",
      //     "url": ""
      //   },]
      //   const accessRoutes = await store.dispatch('permission/generateRoutes', menu)
      //
      //   router.addRoutes(accessRoutes)
      //
      //   console.log(accessRoutes,121)
      //   // return false;
      //   // next()
      //   next({ ...to, replace: true })
      // } else {
      //   try {
      //     // get user info
      //     // await store.dispatch('user/getInfo')
      //
      //     // const menu = await store.dispatch('user/getMenu')
      //     // const accessRoutes = await store.dispatch('permission/generateRoutes', menu)
      //     // router.addRoutes(accessRoutes)
      //     // next({ ...to, replace: true })
      //     next()
      //   } catch (error) {
      //     // remove token and go to login page to re-login
      //     await store.dispatch('user/resetToken')
      //     Message.error(error || 'Has Error')
      //     next(`/login?redirect=${to.path}`)
      //     NProgress.done()
      //   }
      // }
    // }
  } else {
    /* has no token*/

    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
